package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.utils.AliOssUtil;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //首先向setmeal表中添加数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        log.info("获取菜品信息:{}", setmeal);
        setmealMapper.insert(setmeal);

        //获取setmealId
        Long setmealId = setmeal.getId();

        //然后向setmeal_dish表中添加数据
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishList) {
            setmealDish.setSetmealId(setmealId);
        }
        log.info("获取到的套餐内菜品信息为：{}", setmealDishList);
        setmealDishMapper.insertBatch(setmealDishList);
    }


    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开启分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> setmealVOPage = setmealMapper.page(setmealPageQueryDTO);

        return new PageResult(setmealVOPage.getTotal(), setmealVOPage.getResult());
    }


    /**
     * 套餐批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //起售中套餐不能删除
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selectById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE)
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        //删除setmeal中数据
        setmealMapper.deleteBatch(ids);
        //删除setmeal_dish中数据
        setmealDishMapper.deleteBatch(ids);
    }


    /**
     * 根据id查询套餐视图
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectVOById(Long id) {
        //查询除了setmealDishes外所有字段
        SetmealVO setmealVO = setmealMapper.selectVOById(id);
        //查询setmealDishes相关字段
        List<SetmealDish> setmealDishList = setmealDishMapper.selectById(id);
        //赋值
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }


    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //修改setmeal表中数据
        setmealMapper.update(setmeal);

        //修改setmealDish表中数据（先删后改）
        //删除数据
        Long setmealId = setmeal.getId();
        setmealDishMapper.deleteById(setmealId);
        //新增数据
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishList) {
            setmealDish.setSetmealId(setmealId);
        }
        setmealDishMapper.insertBatch(setmealDishList);
    }


    /**
     * 修改套餐状态
     * @param status
     * @param id    id为套餐id
     */
    @Override
    public void updateStatus(String status, Long id) {
        //起售套餐：菜品若停售则无法启用
        if (status.equals("1")){
            //查询套餐id关联的菜品，判断其status
            List<SetmealDish> setmealDishList = setmealDishMapper.selectById(id);
            for (SetmealDish setmealDish : setmealDishList) {
                //查询对应setmealDish对应的dish对象
                Dish dish = dishMapper.getById(setmealDish.getDishId());
                if (dish.getStatus() == StatusConstant.DISABLE)
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }

        //停用套餐无需判断，起售判断完后，可以直接更改状态
        Setmeal setmeal = Setmeal.builder()
                        .status(Integer.valueOf(status))
                        .id(id)
                        .build();
        setmealMapper.update(setmeal);
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
